//
//  MOXLogUntil+Extension.m
//  WCImApp
//
//  Created by alfie on 2020/4/23.
//  Copyright © 2020 WCImApp. All rights reserved.
//

#import "MOXLogUntil+Extension.h"

#include <mars/xlog/xlogger.h>
#include <mars/xlog/appender.h>

#import <mars/xlog/xloggerbase.h>
#import <sys/xattr.h>

#import "WCICache+QAExtension.h"
#import "NSString+QAExtension.h"
#import "QATool.h"
@implementation MOXLogUntil (Extension)

+ (void)LogOffLineWithString:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2) {
    
    va_list args;
    va_start(args, format);
    NSString *rst = [[NSString alloc] initWithFormat:format arguments:args];
    va_end(args);
#if DEBUG
    xlogger2(kLevelDebug, "offline", __XFILE__, __XFUNCTION__, __LINE__, [rst UTF8String]);
#else
    xlogger2(kLevelInfo, "offline", __XFILE__, __XFUNCTION__, __LINE__, [rst UTF8String]);
#endif
}

+ (void)LogNomalWithString:(NSString *)format, ... {
    va_list args;
    va_start(args, format);
    NSString *rst = [[NSString alloc] initWithFormat:format arguments:args];
    va_end(args);

#if DEBUG
    xlogger2(kLevelDebug, "nomal", __XFILE__, __XFUNCTION__, __LINE__, [rst UTF8String]);
#else
    xlogger2(kLevelInfo, "nomal", __XFILE__, __XFUNCTION__, __LINE__, [rst UTF8String]);
#endif

}

+ (void)LogNomalDetailFile:(const char *)file function:(const char *)func line:(int)line string:(NSString *)format, ... {
    va_list args;
    va_start(args, format);
    NSString *rst = [[NSString alloc] initWithFormat:format arguments:args];
    va_end(args);
    
#if DEBUG
    xlogger2(kLevelDebug, "nomal", file, func, line, [rst UTF8String]);
#else
    xlogger2(kLevelInfo, "nomal", file, func, line, [rst UTF8String]);
#endif

}

+ (void)LogNetChangeWithString:(NSString *)format, ... {
    va_list args;
    va_start(args, format);
    NSString *rst = [[NSString alloc] initWithFormat:format arguments:args];
    va_end(args);
    
#if DEBUG
    xlogger2(kLevelDebug, "netChange", __XFILE__, __XFUNCTION__, __LINE__, [rst UTF8String]);
#else
    xlogger2(kLevelInfo, "netChange", __XFILE__, __XFUNCTION__, __LINE__, [rst UTF8String]);
#endif

}

+ (void)LogSocketWithString:(NSString *)format, ... {
    va_list args;
    va_start(args, format);
    NSString *rst = [[NSString alloc] initWithFormat:format arguments:args];
    va_end(args);

#if DEBUG
    xlogger2(kLevelDebug, "socket", __XFILE__, __XFUNCTION__, __LINE__, [rst UTF8String]);
#else
    xlogger2(kLevelInfo, "socket", __XFILE__, __XFUNCTION__, __LINE__, [rst UTF8String]);
#endif
}

+ (void)LogWarningWithString:(NSString *)format, ...{
    va_list args;
    va_start(args, format);
    NSString *rst = [[NSString alloc] initWithFormat:format arguments:args];
    va_end(args);
    
#if DEBUG
    xlogger2(kLevelDebug, "WARNING", __XFILE__, __XFUNCTION__, __LINE__, [rst UTF8String]);
#else
    xlogger2(kLevelInfo, "WARNING", __XFILE__, __XFUNCTION__, __LINE__, [rst UTF8String]);
#endif
}
+ (void)logCategory:(NSString *)category format:(NSString *)format, ... NS_FORMAT_FUNCTION(2, 3) {
    va_list args;
    va_start(args, format);
    NSString *rst = [[NSString alloc] initWithFormat:format arguments:args];
    va_end(args);
#if DEBUG
    xlogger2(kLevelDebug, [category cStringUsingEncoding:NSUTF8StringEncoding], __XFILE__, __XFUNCTION__, __LINE__, [rst UTF8String]);
#else
    xlogger2(kLevelInfo, [category cStringUsingEncoding:NSUTF8StringEncoding], __XFILE__, __XFUNCTION__, __LINE__, [rst UTF8String]);
#endif
}

+ (void)appenderFlush {
    
    NSString *deviceString = [QATool getDeviceInfo];
    [MOXLogUntil LogNomalWithString:@"deviceString  ===  %@", deviceString];
    NSString *iPhoneName = [UIDevice currentDevice].name;
    [MOXLogUntil LogNomalWithString:@"iPhoneName  ===  %@", iPhoneName];

    NSString *appVerion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    [MOXLogUntil LogNomalWithString:@"appVerion  ===  %@n", appVerion];

    NSString *systemName = [UIDevice currentDevice].systemName;
    [MOXLogUntil LogNomalWithString:@"systemName  ===  %@", systemName];

    NSString *systemVersion = [UIDevice currentDevice].systemVersion;
    [MOXLogUntil LogNomalWithString:@"systemVersion  ===  %@", systemVersion];
    if (WCICache.accessToken.length > 0) {
        [MOXLogUntil LogNomalWithString:@"user id  ===  %@", WCICache.user.userId];
        [MOXLogUntil LogNomalWithString:@"user phoneNum  ===  %@", WCICache.user.phone];
    }

    appender_flush();

}

+ (void)appenderClose {
    appender_close();
}

+ (void)openXLog {
    NSString* logPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingString:@"/xlog"];

    // set do not backup for logpath
    const char* attrName = "com.apple.MobileBackup";
    u_int8_t attrValue = 1;
    setxattr([logPath UTF8String], attrName, &attrValue, sizeof(attrValue), 0, 0);
    
    // init xlogger
    #if DEBUG
    xlogger_SetLevel(kLevelDebug);
    appender_set_console_log(true);
    #else
    xlogger_SetLevel(kLevelInfo);
    appender_set_console_log(false);
    #endif
    struct XLogConfig config;
    config.mode_ = kAppenderAsync;
    config.logdir_ = [logPath UTF8String];
    config.nameprefix_ = [[@"JiXinLog" md5] UTF8String];
    config.pub_key_ = "8c96c877f84dd02eeec918f04f4d18b3da5353ceb10b0df78e1e40889a5d120f518b40346bc61126ef5be9e14e438b8c7a7d11e536e3946680ba778071e52f94";
    config.compress_mode_ = kZlib;
    config.compress_level_ = 0;
//        config.cachedir_ = "";
    config.cache_days_ = 0;
    appender_open(config);
}

@end
